<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java NIO Selector：高性能网络编程的核心</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-language);
            position: absolute;
            top: 0;
            right: 0;
            padding: 4px 12px;
            background: rgba(255, 255, 255, 0.1);
            color: #888;
            font-size: 12px;
            border-bottom-left-radius: 8px;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6 leading-tight">
                    Java NIO Selector
                    <span class="block text-3xl mt-2 font-normal opacity-90">高性能网络编程的核心</span>
                </h1>
                <p class="text-xl opacity-90 leading-relaxed">
                    掌握 Selector，解锁高并发网络应用的秘密
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-network-wired mr-2"></i>
                        多路复用
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-bolt mr-2"></i>
                        高性能
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-code mr-2"></i>
                        非阻塞 I/O
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>高并发网络应用开发中，传统的阻塞式 I/O 模型往往难以应对大量连接的场景。Java NIO (New I/O) 的出现为我们提供了非阻塞 I/O 的能力，而其中的 Selector（选择器）更是实现高性能网络编程的核心组件。无论是构建高性能的 Web 服务器、即时通讯系统，还是分布式框架，掌握 Selector 都是必不可少的技能。
            </p>
        </div>

        <!-- Section 1: 一个线程搞定多个连接 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-layer-group mr-3 text-purple-600"></i>
                一个线程搞定多个连接
            </h2>
            
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-700">什么是 Selector？</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-4">
                    Selector 是 Java NIO 中的多路复用器，它允许单个线程监控多个 Channel 的 I/O 状态。简单来说，Selector 就像一个"管家"，负责监控多个"房间"(Channel)的状态，只有当某个房间有"事件"发生时，才会通知你去处理。
                </p>
                <div class="code-block p-4 my-4" data-language="java">
                    <pre><code>// 创建一个 Selector
Selector selector = Selector.open();</code></pre>
                </div>
            </div>

            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-700">Selector 解决了什么问题？</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-4">
                    在传统的阻塞 I/O 模型中，每个连接通常需要一个专门的线程来处理：
                </p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-6">
                    <div class="feature-card">
                        <h4 class="text-xl font-semibold mb-3 text-red-600">
                            <i class="fas fa-times-circle mr-2"></i>传统模型的问题
                        </h4>
                        <ul class="space-y-2 text-gray-600">
                            <li><i class="fas fa-chevron-right mr-2 text-red-400"></i>线程资源消耗过大</li>
                            <li><i class="fas fa-chevron-right mr-2 text-red-400"></i>线程上下文切换开销增加</li>
                            <li><i class="fas fa-chevron-right mr-2 text-red-400"></i>系统性能急剧下降</li>
                        </ul>
                    </div>
                    <div class="feature-card">
                        <h4 class="text-xl font-semibold mb-3 text-green-600">
                            <i class="fas fa-check-circle mr-2"></i>Selector 的优势
                        </h4>
                        <ul class="space-y-2 text-gray-600">
                            <li><i class="fas fa-chevron-right mr-2 text-green-400"></i>单线程处理多连接</li>
                            <li><i class="fas fa-chevron-right mr-2 text-green-400"></i>事件驱动模型</li>
                            <li><i class="fas fa-chevron-right mr-2 text-green-400"></i>大幅提升并发能力</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 2: 工作原理与源码解析 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>
                Selector 的工作原理与源码解析
            </h2>

            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-700">Selector 的内部实现</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-4">
                    Selector 的实现依赖于操作系统的 I/O 多路复用机制，如 Linux 的 epoll、Windows 的 IOCP、macOS 的 kqueue 等。Java NIO 通过 JNI 调用这些底层机制，实现了跨平台的多路复用能力。
                </p>
                
                <!-- Mermaid Diagram -->
                <div class="mermaid my-6">
                    graph TD
                        A[Java Application] --> B[Selector API]
                        B --> C{Operating System}
                        C -->|Linux| D[epoll]
                        C -->|Windows| E[IOCP]
                        C -->|macOS| F[kqueue]
                        D --> G[Hardware]
                        E --> G
                        F --> G
                        style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                        style C fill:#f093fb,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>

            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-700">SelectionKey：事件与通道的桥梁</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-4">
                    SelectionKey 是 Channel 注册到 Selector 后返回的句柄，它包含了 Channel 的信息、感兴趣的事件以及附加的对象等。
                </p>
                
                <div class="highlight-box">
                    <h4 class="text-xl font-semibold mb-3">
                        <i class="fas fa-key mr-2"></i>SelectionKey 的四种事件类型
                    </h4>
                    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mt-4">
                        <div class="bg-white bg-opacity-20 rounded-lg p-4 text-center">
                            <i class="fas fa-plug text-2xl mb-2"></i>
                            <div class="font-semibold">OP_ACCEPT</div>
                            <div class="text-sm opacity-90">接受连接</div>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-4 text-center">
                            <i class="fas fa-link text-2xl mb-2"></i>
                            <div class="font-semibold">OP_CONNECT</div>
                            <div class="text-sm opacity-90">连接就绪</div>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-4 text-center">
                            <i class="fas fa-book-open text-2xl mb-2"></i>
                            <div class="font-semibold">OP_READ</div>
                            <div class="text-sm opacity-90">读就绪</div>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-4 text-center">
                            <i class="fas fa-pen text-2xl mb-2"></i>
                            <div class="font-semibold">OP_WRITE</div>
                            <div class="text-sm opacity-90">写就绪</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 3: 构建高性能网络应用 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-rocket mr-3 text-purple-600"></i>
                使用 Selector 构建高性能网络应用
            </h2>

            <div class="mb-8">
                <h3 class="